<%@page import="cn.yi.newsys.utils.StringUtils"%>
<%@page import="java.io.File"%>
<%@page import="org.apache.commons.fileupload.FileItem"%>
<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@page import="org.apache.commons.fileupload.FileItemFactory"%>
<%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@page import="cn.yi.newsys.pojo.News"%>
<%@page import="cn.yi.newsys.service.impl.NewsServiceImpl"%>
<%@page import="cn.yi.newsys.service.NewsService"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>发布新闻处理页</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
  
  <%
  	int row = -2;
  	boolean isImg = false ;
  	boolean isOnlyText = false ;
  	News news = new News() ;
  	// 判断是否是二进制流的提交方式
  	boolean isMultipart = ServletFileUpload.isMultipartContent(request) ;
  	if(isMultipart){
  		// 创建文件上传所需的 API 实例
  		FileItemFactory factory = new DiskFileItemFactory() ;
  		ServletFileUpload upload = new ServletFileUpload(factory) ;
		// 解析 request 请求，获取 FileItem 对象集合
		List<FileItem> list = upload.parseRequest(request) ;
		// 循环遍历集合中的数据
		for(FileItem item : list){
			// 判断对象中封装的为普通表单
			if(item.isFormField()){		// isFormField 用于判断对象中封装的表单类型  true：普通表单 false：文件表单
				String fieldName = item.getFieldName() ;
				// 赋值
				if("ntitle".equals(fieldName)){
					news.setTittle(item.getString("UTF-8"));
				}else if("nsummary".equals(fieldName)){
					news.setSummary(item.getString("UTF-8")) ;
				}else if("ntid".equals(fieldName)){
					news.setNtid(Integer.valueOf(item.getString("UTF-8"))) ;
				}else if("nauthor".equals(fieldName)){
					news.setAuthor(item.getString("UTF-8")) ;
				}else if("ncontent".equals(fieldName)){
					news.setContent(item.getString("UTF-8")) ;
				}
			}else{			// 文件表单
				// File.separator 文件分隔符
			  	String uploadPath = getServletContext().getRealPath("/") + File.separator + "upload" ;
			  	// 如果不存在，则创建目录
			  	File uploadDir = new File(uploadPath) ;
			  	if(!uploadDir.exists()){
			  		uploadDir.mkdirs() ;
			  	}
				String name = item.getName() ;
				// 控制文件上传的类型
				if(name.endsWith(".jpg") || name.endsWith(".png") || name.endsWith(".bmp") || name.endsWith(".gif")){
					isImg = true ;
				}
				if( name != null && !"".equals(name)){
					if(isImg){
						String fileName = StringUtils.getNewName(name) ;
						// 设置新闻的图片路径
						news.setImgPath(fileName) ;
						// 远程服务器的文件地址
						File file = new File(uploadPath, fileName) ;				
						item.write(file) ;
					}
				}else{
					isOnlyText = true ;
				}
			}
		}
		
		if(isOnlyText || isImg){
			// 创建新闻业务逻辑对象
			NewsService ns = new NewsServiceImpl() ;
			// 设置新闻的发布时间
			news.setDateTime(new Date()) ;
			// 保存新闻
			row = ns.addNews(news) ;
		}
  	}
  	if(row == -1){
   			out.println("<script>alert('发布失败，存在标题相同的新闻') ; location.href='manage/newslist.jsp?test=1';</script>") ;
   		}else if(row == 0){
   			out.println("<script>alert('发布失败，出现未知故障，请稍后再试') ; location.href='manage/newslist.jsp?test=1';</script>") ;
   		}else if(row == -2){
   			out.println("<script>alert('文件格式不正确，发布失败！') ; location.href='manage/newslist.jsp?test=1';</script>") ;
   		}else{
   			out.println("<script>alert('发布成功！') ; location.href='manage/newslist.jsp?test=1';</script>") ;
   		}
  
   %>
   
   <h1>提取到的数据：</h1>
   
   <h2>作者：<%=news.getAuthor() %></h2>
   <h2>标题：<%=news.getTittle() %></h2>
   <h2>内容：<%=news.getContent() %></h2>
   <h2>摘要：<%=news.getSummary() %></h2>
   <h2>类型：<%=news.getNtid() %></h2>
   
   	<%-- <%
   		// 设置编码
   		request.setCharacterEncoding("UTF-8") ;
   		// 获取新闻标题
   		String title = request.getParameter("ntitle") ;
   		// 获取新闻摘要
   		String nsummary = request.getParameter("nsummary") ;
   		// 获取新闻类型
   		Integer ntid = Integer.valueOf(request.getParameter("ntid")) ;
   		// 获取作者
   		String nauthor = request.getParameter("nauthor") ;
   		// 获取新闻内容
   		String ncontent = request.getParameter("ncontent") ;
   		// 获取当前时间
   		Date date = new Date() ;
   		// 创建新闻对象
   		News news = new News() ;
   		news.setAuthor(nauthor);
   		news.setContent(ncontent);
   		news.setDateTime(date);
   		news.setNtid(ntid);
   		news.setSummary(nsummary);
   		news.setTittle(title) ;
   		// 创建新闻业务逻辑对象
   		NewsService ns = new NewsServiceImpl() ;
   		// 发布新闻
   		int row = ns.addNews(news) ;
   		if(row == -1){
   			out.println("<script>alert('发布失败，存在标题相同的新闻') ; location.href='manage/newslist.jsp';</script>") ;
   		}else if(row == 0){
   			out.println("<script>alert('发布失败，出现未知故障，请稍后再试') ; location.href='manage/newslist.jsp';</script>") ;
   		}else{
   			out.println("<script>alert('发布成功！') ; location.href='manage/newslist.jsp';</script>") ;
   		}
   		
   	
   	 %> --%>
  </body>
</html>
